package servlets;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class BasketServlet extends HttpServlet 
{
	private static final long serialVersionUID = 2L;
	private String[] products;
	private List<String> productIDCollection = new ArrayList<String>();

	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException 
	{
		doProcessing(req, resp);
	}
	
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException 
	{
		doProcessing(req, resp);
	}
	
	private void doProcessing(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException
	{
		products = req.getParameterValues("selectedProduct");
		for(String IDs : products)
		{
			productIDCollection.add(IDs);
		}
		String[] productIDArray = new String[productIDCollection.size()];
		productIDArray = productIDCollection.toArray(productIDArray);
		req.getSession().setAttribute("products", productIDArray);
		
		RequestDispatcher dispatcher = req.getRequestDispatcher("basket.jsp");
		dispatcher.forward(req, resp);
	}
}
